# coding = utf-8
import logging
from django.db.models import Q
from gaokao.api.serializers import CollegeAdmissionSerializer, CollegeSerializer, MajorSerializer, AreaSerializer, \
    ProvinceSerializer, CitySerializer, WsCollegeAdmissionSerializer, WsCollegeSerializer, WsMajorSerializer, \
    WsAreaSerializer, WsProvinceSerializer, WsCitySerializer
from gaokao.models import Yxzyxxb_lg, Yxzyxxb_ws
from rest_framework import filters
from rest_framework.generics import ListAPIView
from rest_framework.permissions import AllowAny
from rest_framework.viewsets import ReadOnlyModelViewSet
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page

logger = logging.getLogger("django")


class CollegeListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_lg.objects.order_by().values("yxmc").distinct()
    serializer_class = CollegeSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    def list(self, request, *args, **kwargs):
        return super(CollegeListAPIView, self).list(request, *args, **kwargs)


class MajorListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_lg.objects.order_by().values("zymc").distinct()
    serializer_class = MajorSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)


class AreaListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_lg.objects.order_by("dqmc").values("dqmc").distinct()
    serializer_class = AreaSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    # def list(self, request, *args, **kwargs):
    #     return super().list(request, *args, **kwargs)


class ProvinceListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_lg.objects.order_by("szsf").values("szsf").distinct()
    serializer_class = ProvinceSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    # def list(self, request, *args, **kwargs):
    #     return super().list(request, *args, **kwargs)


class CityListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_lg.objects.order_by("xxszd").values("xxszd").distinct()
    serializer_class = CitySerializer

    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)


class CollegeAdmissionViewSet(ReadOnlyModelViewSet):
    authentication_classes = ()
    permission_classes = [AllowAny]
    queryset = Yxzyxxb_lg.objects.all()
    serializer_class = CollegeAdmissionSerializer
    filter_backends = [filters.OrderingFilter]
    ordering_fields = ['yxpinyin', 'zypinyin', 'zdf1', 'pmmc', 'zypm']

    def get_queryset(self):
        queryset = super(CollegeAdmissionViewSet, self).get_queryset()
        yxmc = self.request.query_params.get("yxmc", None)
        yxmc1 = self.request.query_params.getlist("yxmc1[]", None)
        lqpc = self.request.query_params.getlist("lqpc[]", None)
        dqmc = self.request.query_params.getlist("dqmc[]", None)
        szsf = self.request.query_params.getlist("szsf[]", None)
        xxszd = self.request.query_params.getlist("xxszd[]", None)
        syljs = self.request.query_params.getlist("syljs[]", None)
        bxxz = self.request.query_params.getlist("bxxz[]", None)
        zymc = self.request.query_params.get("zymc", None)
        zymc1 = self.request.query_params.getlist("zymc1[]", None)
        yxlx = self.request.query_params.getlist("yxlx[]", None)
        wyyz = self.request.query_params.getlist("wyyz[]", None)
        wymc = self.request.query_params.get("wymc", None)
        xz = self.request.query_params.getlist("xz[]", None)
        xf = self.request.query_params.get("xf", None)
        zylb = self.request.query_params.getlist("zylb[]", None)
        no_zymc = self.request.query_params.get("no_zymc", None)
        bz = self.request.query_params.get("bz", None)
        jbw = self.request.query_params.get("jbw", None)
        eyy = self.request.query_params.get("eyy", None)
        zybs = self.request.query_params.get("zybs", None)
        qjjh = self.request.query_params.get("qjjh", None)
        gzdz = self.request.query_params.get("gzdz", None)
        sylzy = self.request.query_params.get("sylzy", None)
        tjsx = self.request.query_params.get("tjsx", None)
        xzzy = self.request.query_params.get("xzzy", None)
        # 分数
        from_score = self.request.query_params.get("from_score", None)
        to_score = self.request.query_params.get("to_score", None)
        # 位次
        from_pos = self.request.query_params.get("from_pos", None)
        to_pos = self.request.query_params.get("to_pos", None)

        # 985
        if jbw == "true":
            queryset = queryset.filter(jbw='Y')
        # 211
        if eyy == "true":
            queryset = queryset.filter(eyy='Y')
        # 部属
        if zybs == "true":
            queryset = queryset.filter(zybs='Y')
        # 强基院校
        if qjjh == "true":
            queryset = queryset.filter(qjjh='Y')
        # 单招
        if gzdz == "true":
            queryset = queryset.filter(gzdz='Y')
        # 一流专业
        if sylzy == "true":
            queryset = queryset.filter(sylzy='Y')
        # 条件受限
        if tjsx == "true":
            queryset = queryset.filter(tjsx='Y')
        # 包含新增
        if xzzy == "true":
            queryset = queryset.filter(xzzy='Y')
        # 院校包含
        if yxmc:
            queryset = queryset.filter(yxmc__contains=yxmc)
        # 院校名称
        if yxmc1:
            queryset = queryset.filter(yxmc__in=yxmc1)
        #  录取批次
        if lqpc:
            queryset = queryset.filter(lqpc__in=lqpc)
        # 地区名称
        if dqmc:
            queryset = queryset.filter(dqmc__in=dqmc)
        # 所在省份
        if szsf:
            queryset = queryset.filter(szsf__in=szsf)
        # 所在地
        if xxszd:
            queryset = queryset.filter(xxszd__in=xxszd)
        # 一流院校
        if syljs:
            queryset = queryset.filter(syljs__in=syljs)
        # 办学性质
        if bxxz:
            queryset = queryset.filter(bxxz__in=bxxz)
        # 专业名称包含
        if zymc:
            queryset = queryset.filter(zymc__contains=zymc)
        # 专业名称
        if zymc1:
            queryset = queryset.filter(zymc__in=zymc1)
        # 专业不包含
        if no_zymc:
            queryset = queryset.exclude(zymc__contains=no_zymc)
        # 院校类型
        if yxlx:
            queryset = queryset.filter(yxlx__in=yxlx)
        # 语种
        if wyyz:
            queryset = queryset.filter(wyyz__in=wyyz)
        # 语种包含
        if wymc:
            queryset = queryset.filter(wyyz__contains=wymc)
        # 学制
        if xz:
            queryset = queryset.filter(xz__in=xz)
        # 学费少于
        if xf:
            queryset = queryset.filter(xf__lte=xf)
        # 专业类别
        if zylb:
            queryset = queryset.filter(zylb__in=zylb)
        # 备注包含
        if bz:
            queryset = queryset.filter(bz__contains=bz)

        if from_score and to_score:
            from_score = int(from_score)
            to_score = int(to_score)
            if to_score > from_score > 0:
                queryset = queryset.filter(zdf1__range=[from_score, to_score])

        if from_pos and to_pos:
            from_pos = int(from_pos)
            to_pos = int(to_pos)
            if from_pos > to_pos > 0:
                queryset = queryset.filter(zyzdwc1__range=[from_pos, to_pos])

        return queryset


class WsCollegeAdmissionViewSet(ReadOnlyModelViewSet):
    authentication_classes = ()
    permission_classes = [AllowAny]
    queryset = Yxzyxxb_ws.objects.all()
    serializer_class = WsCollegeAdmissionSerializer
    filter_backends = [filters.OrderingFilter]
    ordering_fields = ['yxmc', 'zymc', 'zdf1', 'pmmc', 'zypm']

    def get_queryset(self):
        queryset = super(WsCollegeAdmissionViewSet, self).get_queryset()
        yxmc = self.request.query_params.get("yxmc", None)
        yxmc1 = self.request.query_params.getlist("yxmc1[]", None)
        lqpc = self.request.query_params.getlist("lqpc[]", None)
        dqmc = self.request.query_params.getlist("dqmc[]", None)
        szsf = self.request.query_params.getlist("szsf[]", None)
        xxszd = self.request.query_params.getlist("xxszd[]", None)
        syljs = self.request.query_params.getlist("syljs[]", None)
        bxxz = self.request.query_params.getlist("bxxz[]", None)
        zymc = self.request.query_params.get("zymc", None)
        zymc1 = self.request.query_params.getlist("zymc1[]", None)
        yxlx = self.request.query_params.getlist("yxlx[]", None)
        wyyz = self.request.query_params.getlist("wyyz[]", None)
        wymc = self.request.query_params.get("wymc", None)
        xz = self.request.query_params.getlist("xz[]", None)
        xf = self.request.query_params.get("xf", None)
        zylb = self.request.query_params.getlist("zylb[]", None)
        no_zymc = self.request.query_params.get("no_zymc", None)
        bz = self.request.query_params.get("bz", None)
        jbw = self.request.query_params.get("jbw", None)
        eyy = self.request.query_params.get("eyy", None)
        zybs = self.request.query_params.get("zybs", None)
        qjjh = self.request.query_params.get("qjjh", None)
        gzdz = self.request.query_params.get("gzdz", None)
        sylzy = self.request.query_params.get("sylzy", None)
        tjsx = self.request.query_params.get("tjsx", None)
        xzzy = self.request.query_params.get("xzzy", None)

        # 985
        if jbw == "true":
            queryset = queryset.filter(jbw='Y')
        # 211
        if eyy == "true":
            queryset = queryset.filter(eyy='Y')
        # 部属
        if zybs == "true":
            queryset = queryset.filter(zybs='Y')
        # 强基院校
        if qjjh == "true":
            queryset = queryset.filter(qjjh='Y')
        # 单招
        if gzdz == "true":
            queryset = queryset.filter(gzdz='Y')
        # 一流专业
        if sylzy == "true":
            queryset = queryset.filter(sylzy='Y')
        # 条件受限
        if tjsx == "true":
            queryset = queryset.filter(tjsx='Y')
        # 包含新增
        if xzzy == "true":
            queryset = queryset.filter(xzzy='Y')
        # 院校包含
        if yxmc:
            queryset = queryset.filter(yxmc__contains=yxmc)
        # 院校名称
        if yxmc1:
            queryset = queryset.filter(yxmc__in=yxmc1)
        #  录取批次
        if lqpc:
            queryset = queryset.filter(lqpc__in=lqpc)
        # 地区名称
        if dqmc:
            queryset = queryset.filter(dqmc__in=dqmc)
        # 所在省份
        if szsf:
            queryset = queryset.filter(szsf__in=szsf)
        # 所在地
        if xxszd:
            queryset = queryset.filter(xxszd__in=xxszd)
        # 一流院校
        if syljs:
            queryset = queryset.filter(syljs__in=syljs)
        # 办学性质
        if bxxz:
            queryset = queryset.filter(bxxz__in=bxxz)
        # 专业名称包含
        if zymc:
            queryset = queryset.filter(zymc__contains=zymc)
        # 专业名称
        if zymc1:
            queryset = queryset.filter(zymc__in=zymc1)
        # 专业不包含
        if no_zymc:
            queryset = queryset.exclude(zymc__contains=no_zymc)
        # 院校类型
        if yxlx:
            queryset = queryset.filter(yxlx__in=yxlx)
        # 语种
        if wyyz:
            queryset = queryset.filter(wyyz__in=wyyz)
        # 语种包含
        if wymc:
            queryset = queryset.filter(wyyz__contains=wymc)
        # 学制
        if xz:
            queryset = queryset.filter(xz__in=xz)
        # 学费少于
        if xf:
            queryset = queryset.filter(xf__lte=xf)
        # 专业类别
        if zylb:
            queryset = queryset.filter(zylb__in=zylb)
        # 备注包含
        if bz:
            queryset = queryset.filter(bz__contains=bz)

        return queryset


class WsCollegeListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_ws.objects.order_by().values("yxmc").distinct()
    serializer_class = WsCollegeSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    def list(self, request, *args, **kwargs):
        return super(WsCollegeListAPIView, self).list(request, *args, **kwargs)


class WsMajorListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_ws.objects.order_by().values("zymc").distinct()
    serializer_class = WsMajorSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)


class WsAreaListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_ws.objects.order_by("dqmc").values("dqmc").distinct()
    serializer_class = WsAreaSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    # def list(self, request, *args, **kwargs):
    #     return super().list(request, *args, **kwargs)


class WsProvinceListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_ws.objects.order_by("szsf").values("szsf").distinct()
    serializer_class = WsProvinceSerializer

    # @method_decorator(cache_page(60 * 60 * 2))
    # def list(self, request, *args, **kwargs):
    #     return super().list(request, *args, **kwargs)


class WsCityListAPIView(ListAPIView):
    authentication_classes = ()
    permission_classes = [AllowAny]
    pagination_class = None
    queryset = Yxzyxxb_ws.objects.order_by("xxszd").values("xxszd").distinct()
    serializer_class = WsCitySerializer

    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)